package cn.wsalix.spring;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

@Component("springUtils")
public class SpringUtils implements ApplicationContextAware {// extends
																// ApplicationObjectSupport
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static ApplicationContext applicationContext; // Spring应用上下文环境

	/**
	 * @param clazz
	 *            RequestMapping.class
	 */
	public void traverse(Map<String, Object> annotationBeans) {
		for (Object bean : annotationBeans.values()) {
			logger.info(bean.getClass().getName());
			Method[] methods = bean.getClass().getDeclaredMethods();
			for (Method method : methods) {// 问题,获取不到注解RequestMapping=注：已解决
				RequestMapping requestMapping = method
						.getAnnotation(RequestMapping.class);
			/*	AuthDatas AuthDatas = method
						.getAnnotation(AuthDatas.class);
				RequiresPermissions requiresPermissions = method
						.getAnnotation(RequiresPermissions.class);*/
				
			}
			
			
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		SpringUtils.applicationContext = applicationContext;
	}

	public <T extends Annotation> Map<String, Object> getBeansWithAnnotation(Class<T> clazz) {
		Map<String, Object> annotationBeans = applicationContext
				.getBeansWithAnnotation(clazz);
		return annotationBeans;
	}
}
